package net.huashitong.taskjob.service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.dao.SysDeptDao;
import com.sirdc.modules.sys.entity.SysDept;
import com.sirdc.modules.sys.entity.SysUser;
import com.sirdc.modules.sys.service.SysTableService;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Indicators.entity.TertiaryIndustry;
import net.huashitong.Indicators.service.TertiaryIndustryService;
import net.huashitong.taskjob.dao.TaskJobDao;
import net.huashitong.taskjob.entity.TaskJob;
import net.huashitong.taskjob.filter.TaskJobFilter;
import net.huashitong.taskjob.util.UtilsTools;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.List;

@Service
public class TaskJobService extends StringPKService<TaskJob> {
    @Autowired
    private TaskJobDao dao;

    @Autowired
    private SysTableService sysTableService;
    @Autowired
    private SysDeptDao sysDeptDao;
    @Autowired
    private TertiaryIndustryService tertiaryIndustryService;
    @Autowired
    private ResDpteService resDpteService;
    @Autowired
    private SupervisionService supervisionService;

    @Override
    protected void beforeSave(TaskJob obj) {
        obj.setSysId(sysTableService.updateMaxSysId("t_task_job", null));
    }

    @Override
    protected BaseDao<TaskJob, String> getDao() {
        return dao;
    }

    /**
     * 查询任务主要入口
     *
     * @param filter
     * @return
     */
    public List<TaskJob> getTaskLists(TaskJobFilter filter) {
        return dao.getMissinTask(filter);
    }
    /**
     * ------------保存手动新建任务-----------
     */
    public void saveTaskJob(TaskJobFilter filter) {
        TaskJob task = new TaskJob();
        BeanUtils.copyProperties(filter, task);
        task.setTaskTimes("1"); //督查次数默认1
        task.setAaa001("1");
        task.setTaskType("1"); //手动创建任务
        task.setTaskResult("0"); //督查任务情况默认为0未完成
        task.setSupervisionResult("0");//督查组完成情况默认为0未完成
        task.setSupervisionScore("0.0");//督查组得分默认为0.0
        task.setRespResult("0");//责任单位完成情况默认为0
        task.setRespScore("0.0");//责任单位得分默认为0.0

        if(StringUtils.isBlank(filter.getStartDate())){
            task.setStartDate(UtilsTools.getCurrentDate()); // 任务开始时间默认为当前日期
        }
        if(StringUtils.isBlank(filter.getPlanStartDate())){
            task.setPlanStartDate(UtilsTools.getCurrentDate()); // 任务计划开始时间默认为当前日期
        }

        //指标信息
        if(StringUtils.isNotBlank(filter.getIndustryId())){
            TertiaryIndustry industry = tertiaryIndustryService.getById(filter.getIndustryId());
            if(ObjectUtils.isNotBlank(industry)){
                task.setIndustryName(industry.getIndicatorName());

            }
        }
        //督查组信息
        if(StringUtils.isNotBlank(filter.getGroupCode())){
            //查询督查组
            SysDept sysDept = sysDeptDao.getById(filter.getGroupCode());
            if(ObjectUtils.isNotBlank(sysDept)){
                task.setGroupName(sysDept.getDeptName());
                //查询督查组成员并赋值
                List<SysUser> resUsers = supervisionService.querySupervisionUserByDeptId(sysDept.getSysId());
                task.setInspectors(resUsers.get(0).getName());
                task.setPhones(resUsers.get(0).getMobile());
            }
        }
        //责任单位信息
        if(StringUtils.isNotBlank(filter.getDeptId())){
            //查询责任单位
            SysDept sysDept = sysDeptDao.getById(filter.getDeptId());
            if(ObjectUtils.isNotBlank(sysDept)){
                task.setResDept(sysDept.getDeptName());
                //查询责任人并赋值
                List<SysUser> resUsers = resDpteService.queryResUserByDeptId(sysDept.getSysId());
                task.setResUser(resUsers.get(0).getName());
                task.setResPhone(resUsers.get(0).getMobile());
                task.setUserId(resUsers.get(0).getSysId());
            }
        }
        task.setIsFeedback("0");//是否反馈攻坚办
        task.setIsRebuild("0");//是否重新督查
        //发送短信
        task.setAaa002("1");
        //手动创建任务需要立刻发送短息至相关人员
        save(task);
    }

    /**
     * -------------------修改任务--------------
     *
     * @param filter
     * @param task
     */
    public void updateTaskJob(TaskJobFilter filter, TaskJob task) {
        //前端可修改的参数
        if(StringUtils.isNotBlank(filter.getGroupCode())){
            SysDept group = sysDeptDao.getById(filter.getGroupCode());
            task.setGroupCode(filter.getGroupCode());
            task.setGroupName(group.getDeptName());
        }
        if(StringUtils.isNotBlank(filter.getInspectors())){
            task.setInspectors(filter.getInspectors());
        }
        if(StringUtils.isNotBlank(filter.getPhones())){
            task.setPhones(filter.getPhones());
        }
        if(StringUtils.isNotBlank(filter.getPlanStartDate())){
            task.setPlanStartDate(filter.getPlanStartDate());
        }else{
            task.setPlanStartDate(UtilsTools.getCurrentDate());
        }
        if(StringUtils.isNotBlank(filter.getPlanEndDate())){
            task.setPlanEndDate(filter.getPlanEndDate());
        }
        if(StringUtils.isNotBlank(filter.getDeptId())){
            SysDept group = sysDeptDao.getById(filter.getDeptId());
            task.setDeptId(filter.getDeptId());
            task.setResDept(group.getDeptName());
        }
        if(StringUtils.isNotBlank(filter.getResPhone())){
            task.setResPhone(filter.getResPhone());
        }
        if(StringUtils.isNotBlank(filter.getResUser())){
            task.setResUser(filter.getResUser());
        }
        if(StringUtils.isNotBlank(filter.getStartDate())){
            task.setStartDate(filter.getStartDate());
        }
        if(StringUtils.isNotBlank(filter.getTaskContent())){
            task.setTaskContent(filter.getTaskContent());
        }
        if(StringUtils.isNotBlank(filter.getTaskName())){
            task.setTaskName(filter.getTaskName());
        }
        if(StringUtils.isNotBlank(filter.getEndDate())){
            task.setEndDate(filter.getEndDate());
        }
//        BeanUtils.copyProperties(filter, task);
        update(task);
    }
}
